package com.logica.tutorial.web;

import org.apache.wicket.behavior.Behavior;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.validation.IValidatable;
import org.apache.wicket.validation.IValidator;
import org.apache.wicket.validation.ValidationError;

import com.logica.tutorial.service.PersonService;

public class PersonIdValidator extends Behavior implements IValidator<Long>   {

	private static final long serialVersionUID = 3246220582209163458L;

	@SpringBean
	PersonService personService;
	
	public PersonIdValidator() {
		super();
	}

	@Override
	public void validate(IValidatable<Long> validatable) {
		if(personService.isIdInUse((Long) validatable.getValue())) {
			ValidationError error = new ValidationError(this, "inUse");
			validatable.error(error);
		}			
	}
}
